﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace AcctrueWMS.Foundation.Util
{
    public class WordFilter
    {
        static object mutex = new object();
        int[] charMap=new int[65536];
        List<string> badwordDic = new List<string>();
        string[] badwords;
        private static WordFilter instance;

        private WordFilter()
        { }

        /// <summary>
        /// 使用过滤词初始化过滤词组件
        /// </summary>
        /// <param name="badwords"></param>
        private WordFilter(string[] badwords)
        {
            this.badwords = badwords;
            foreach(string badword in badwords)
            {
                for (int i = 0; i < badword.Length; i++)
                    charMap[badword[i]] |= (int)1 << i;
                if(!badwordDic.Contains(badword))
                    badwordDic.Add(badword);
            }
        }

        public static WordFilter GetInstance(string[] badwords)
        {
            lock (mutex)
            {
                if (instance == null)
                {
                    instance = new WordFilter(badwords);
                    return instance;
                }
                else 
                {
                    return instance;
                }
            }
        }

        /// <summary>
        /// 过滤文本，并加上开始结束符
        /// </summary>
        /// <param name="text">文本</param>
        /// <param name="startToken">开始符</param>
        /// <param name="endToken">结束符</param>
        /// <returns>过滤后的文本</returns>
        public string Filter(string text, string startToken, string endToken)
        {
            StringBuilder sBuilder=new StringBuilder();
            StringBuilder wBuilder = new StringBuilder();
            int i,j,k,l;
            for (i=0; i < text.Length;)
            {
                for (j = 0; j +i< text.Length; j++)
                {
                    if ((charMap[text[i + j]] & (1 << j)) == 0)
                        break;
                }
                if (j > 0)
                {
                    for (k = 0; k < j; k++)
                        wBuilder.Append(text[i + k]);
                    for (k = j; k > 0; k--)//找出符合charMap位置映射的单词，然后从词典中通过hash算法验证是否是过滤词
                    {
                        if (badwordDic.Contains(wBuilder.ToString()))//在词典中
                        {
                            sBuilder.Append(startToken);
                            sBuilder.Append(wBuilder.ToString());
                            sBuilder.Append(endToken);
                            wBuilder.Remove(0, wBuilder.Length);
                            break;
                        }
                        else
                            wBuilder.Remove(k - 1, 1);
                    }
                    for (l = k; l < j; l++)
                        sBuilder.Append(text[i + l]);

                    i += l;
                }
                else
                {
                    sBuilder.Append(text[i++]);
                }
            }
            return sBuilder.ToString();
        }
    }
}
